import { Message } from "@arco-design/web-vue"
import { evaluate } from "mathjs"

/**
 *判断对象是否是一个纯粹的对象
 */
export function isPlainObject(obj) {
  return typeof obj === "object" && Object.prototype.toString.call(obj) === "[object Object]"
}
/**
 *深度合并两个对象的方法
 */
export function deepAssign(target, source) {
  if (isPlainObject(source)) {
    if (!isPlainObject(target)) {
      target = {}
    }
    for (const s in source) {
      if (s === "__proto__" || target === source[s]) {
        continue
      }
      if (isPlainObject(source[s])) {
        target[s] = deepAssign(target[s], source[s])
      } else {
        target[s] = source[s]
      }
    }
    return target
  }
}

function getPercent(num, total) {
  /// <summary>
  /// 求百分比
  /// </summary>
  /// <param name="num">当前数</param>
  /// <param name="total">总数</param>
  num = parseFloat(num)
  total = parseFloat(total)
  if (isNaN(num) || isNaN(total)) {
    return "-"
  }
  return total <= 0 ? 0 : Math.round((num / total) * 10000) / 100.0
}

// 数组对象更换key
export const arrReplaceKeys = (arr, keyMap) => {
  if (!arr || !arr.length) {
    // Message.warning("数据为空")
    // return []
  }
  if (arr.length > 10000) {
    Message.warning("数据量过大")
    return false
  }
  const sum = {}
  Object.values(keyMap).forEach((item) => {
    try {
      if (item.format.includes("PERCENT")) {
        sum[item.title] = g.sum(arr.map((v) => +v[item.original_field]))
      }
    } catch (e) {
      /* empty */
    }
  })
  const obj = {}
  arr.forEach((item) => {
    for (const key in item) {
      obj[keyMap[key].original_field] = item[key]
    }
  })
  const list = arr.map((item) => {
    const data = {}
    for (const key in item) {
      data[keyMap[key].title] = !keyMap[key].isDimension ? +item[key] : item[key]
      try {
        if (keyMap[key].format.includes("PERCENT")) {
          const toFixed = g.getNum(keyMap[key].format) || 0
          data[keyMap[key].title] = getPercent(data[keyMap[key].title], sum[keyMap[key].title]).toFixed(toFixed)
        }
      } catch (e) {
        /* empty */
      }
      if (keyMap[key].format.includes("CUSTOM")) {
        if (keyMap[key].official) {
          try {
            let expression = `${data[keyMap[key].title] * 1}${keyMap[key].official}`
            for (const expressionKey in obj) {
              expression = g.replaceAll(expression, `${expressionKey}`, obj[expressionKey] || 0)
            }
            data[keyMap[key].title] = evaluate(expression)
          } catch (e) {
            data[keyMap[key].title] = 0
          }
        }
        if (keyMap[key].digits) {
          data[keyMap[key].title] = data[keyMap[key].title].toFixed(keyMap[key].digits)
        }
      }
    }
    return data
  })
  return list
}

/*
 * 渐变色提取
 * linear-gradient(90deg,rgb(241, 29, 0) 0%,rgb(73, 106, 220) 100%)
 */
export const gradientColor = (str) => {
  // 切割gradient(
  const strold = g.Copy(str)
  try {
    const deg = strold.split("gradient(")[1].split("deg")[0]
    const regex = /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/g
    const colors = []
    let match
    while ((match = regex.exec(str)) !== null) {
      const rgba = `rgba(${match[1]},${match[2]},${match[3]},${match[4] || 1})`
      colors.push(rgba)
    }
    colors[2] = +deg
    return colors
  } catch (e) {
    return []
  }
}
